﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace kim.Ajax
{
    public partial class ajax : System.Web.UI.Page
    {
        private string _oper = string.Empty;
        private string _response = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            GetAjax();
        }
        protected void GetAjax()
        {
            string oq = kim.Common.KIMRequest.GetString("action");
            if (oq == null)
            {
                Response.StatusCode = 404;
                Response.End();
                return;
            }
            _oper = oq;
            switch (_oper)
            {

                case "gethonor": GetHonor(); break;
                case "getpt": GetXy(); break;
                case "getschool": GetSchool(); break;
                default: DefResp(); break;
            }
            Response.ContentType = "application/json";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
           Response.Write(_response);
            Response.End();
        }
        private void GetSchool()
        {
            int nid = GetQueryInt("nid");
            string result = string.Empty;
            if (nid == 0)
            {
                this._response = "{\"RScode\" :\"0\" ,\"RSresult\" :\"ptid is null\"}";
                return;
            }
            Model.eg_news moNews = new BLL.eg_news().GetModel(nid);
            if (moNews == null)
            {
                this._response = "{\"RScode\" :\"0\" ,\"RSresult\" :\"moNews is null\"}";
            }
            else {
                result = "{\"RScode\" :\"1\", \"_nid\" :\""+moNews.ID+"\",";
                result += "\"_title\" :\"" + moNews.Name + "\" ,";
                result += "\"_jj\" :\"" +  kim.Common.Utils.DropHTML(moNews.jj,280) + "\" ,";
                result += "\"_pic\" :\"" + moNews.zjj + "\" ,";
            }
            IList<Model.eg_news> molist = new BLL.eg_news().GetModelList("ClassID="+nid);
            int i = 1;
            if (molist.Count > 0)
            {
                result += "\"RSitem\" :[";
               
                foreach(Model.eg_news mo in molist)
                {
                    result += "{";
                    result += "\"_i\" :\"" + mo.ID + "\" ,";
                    result += "\"_t\" :\"" + mo.Name + "\" ,";
                    result += "\"_con\" :\"" + kim.Common.Utils.DropHTML(mo.Content,100) + "\" ,";
                    result += "\"_pic\" :\"" + mo.Pic + "\" ,";
                    result += "\"author\" :\"by kim\"";
                    result += "}";
                    if (i != molist.Count) result += ",";
                    i++;
                }
               
                result += "]}";
              
            }
            else {
                result += "\"_Rzy\" :\"0\"";
                result += "}";
            }
            this._response = result;
           
        }
        private void GetXy()
        {
            int typeid = GetQueryInt("typeid");
            string result = string.Empty;
            if (typeid == 0)
            {
                this._response = "{\"RScode\" :\"0\" ,\"RSresult\" :\"ptid is null\"}";
                return;
            }
            int i=1;
            List<Model.Product> moList = new BLL.Product().GetModelList("ClassID=" + typeid);
            if (moList.Count > 0)
            {
                result = "{\"RScode\" :\"1\" ,\"RSitem\" :[";
                foreach (Model.Product mo in moList)
                {
                    result += "{";
                    result += "\"_i\" :\"" +mo.ID + "\" ,";
                    result += "\"_t\" :\"" +mo.Name + "\" ,";
                    result += "\"_time\" :\"" + ((DateTime)mo.InTime).ToString("yyyy-MM-dd") + "\" ,";
                    result += "\"author\" :\"by kim\"";
                    result += "}";
                    
                    if (i!= moList.Count) result += ",";
                    i++;
                }
                result += "]}";
                this._response = result;
            }
            else
            {
                this._response = "{\"RScode\" :\"0\" ,\"RSresult\" :\"mo is null\"}";
            }

           

        }
      
        protected void DefResp()
        {
            this._response = "{\"RScode\" :\"404\" ,\"RSresult\" :\"FORBID\"}";
        }
        private void GetHonor()
        {
            int id = GetQueryInt("id");
            if (id == 0)
            {
                this._response = "{\"RScode\" :\"0\" ,\"RSresult\" :\"ptid is null\"}";
                return;
            }
            Model.eg_news mo = new BLL.eg_news().GetModel(id);
            if (mo != null && mo.Pic!="" &&mo.Pic!=null)
            {
                this._response = "{\"RScode\" :\"1\" ,\"RSrc\" :\""+mo.Pic+"\"}";
            }
            else
                this._response = "{\"RScode\" :\"1\" ,\"RSresult\" :\"mo is null\"}";
        }

        public string GetQueryStr(string strName)
        {
            if (HttpContext.Current.Request.QueryString[strName] == null)
                return "";
            return HttpContext.Current.Request.QueryString[strName].ToString();
        }
        public int GetQueryInt(string strName)
        {
            if (HttpContext.Current.Request.QueryString[strName] == null)
                return 0;
            if (HttpContext.Current.Request.QueryString[strName].ToString() == "")
                return 0;
            return Convert.ToInt32(HttpContext.Current.Request.QueryString[strName].ToString());
        }
    }
}